﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Controls;

namespace ChoDiaOc_HK_WPF.Views.QuanLy {
    /// <summary>
    /// Interaction logic for QuanLyLoaiDiaOc.xaml
    /// </summary>
    public partial class QuanLyLoaiDiaOc : UserControl {
        public QuanLyLoaiDiaOc() {
            InitializeComponent();
            entities = new DBEntities();

        }

        private void btnThem_Click(object sender, RoutedEventArgs e) {
            if (lOAIDIAOCsListView.SelectedIndex != -1) {
                lOAIDIAOCsListView.SelectedIndex = -1;
            } else {
                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {

                    LOAIDIAOC dn = new LOAIDIAOC();
                    dn.MaLoaiDiaOc = TiepNhanDangKyController.GetInstance().GenerateMaLOAIDIAOC();
                    dn.TenLoaiDiaOc = tenLoaiDiaOcTextBox.Text;
                    dn.KyHieu = kyHieuTextBox.Text;

                    entities.AddToLOAIDIAOCs(dn);
                    entities.SaveChanges();
                    System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["lOAIDIAOCsViewSource"];
                    entities = new DBEntities();
                    myCollectionViewSource.Source = entities.LOAIDIAOCs;
                }
            }

        }

        private void btnXoa_Click(object sender, RoutedEventArgs e) {
            if (lOAIDIAOCsListView.SelectedIndex != -1) {
                LOAIDIAOC dn = lOAIDIAOCsListView.SelectedItem as LOAIDIAOC;

                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {
                    entities.LOAIDIAOCs.DeleteObject(lOAIDIAOCsListView.SelectedItem as LOAIDIAOC);
                    entities.SaveChanges();
                }
            }
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e) {
            try {
                entities.SaveChanges();
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            } catch (Exception exp) {
                MessageBox.Show(exp.Message);
            }

        }

        private void btnNhapLai_Click(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["lOAIDIAOCsViewSource"];
            entities = new DBEntities();
            myCollectionViewSource.Source = entities.LOAIDIAOCs;

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["lOAIDIAOCsViewSource"];
            myCollectionViewSource.Source = entities.LOAIDIAOCs;

        }
        
        private DBEntities entities;

    }
}
